
using LibraryManagementSystem.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

using Microsoft.AspNetCore.Authorization;


namespace LibraryManagementSystem.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class MemberController : ControllerBase
    {
        private readonly LibraryContext _context;

        public MemberController(LibraryContext context)
        {
            _context = context;
        }
        

        // GET: api/member/search
        [HttpGet("search")]
        public async Task<ActionResult<IEnumerable<Member>>> SearchMembers(string? name, int? id, string? type)
        {
            var query = _context.Members.AsQueryable();

            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(m => m.Name.Contains(name));
            }

            if (id.HasValue)
            {
                query = query.Where(m => m.Id == id.Value);
            }

            if (!string.IsNullOrEmpty(type))
            {
                query = query.Where(m => m.MemberType == type);
            }

            return await query.ToListAsync();
        }

        // PUT: api/member/update/{id}
        [HttpPut("update/{id}")]
        public async Task<IActionResult> UpdateMember(int id, [FromBody] Member updatedMember)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var member = await _context.Members.FindAsync(id);
            if (member == null)
            {
                return NotFound();
            }

            member.Name = updatedMember.Name;
            member.Contact = updatedMember.Contact;
            member.MemberType = updatedMember.MemberType;

            await _context.SaveChangesAsync();
            return Ok(member);
        }

        // DELETE: api/member/delete/{id}
        [HttpDelete("delete/{id}")]
        public async Task<IActionResult> DeleteMember(int id)
        {
            var member = await _context.Members.FindAsync(id);
            if (member == null)
            {
                return NotFound();
            }

            _context.Members.Remove(member);
            await _context.SaveChangesAsync();
            return NoContent();
        }

        // GET: api/member/borrowhistory/{id}
        [HttpGet("borrowhistory/{id}")]
        public async Task<ActionResult<IEnumerable<BorrowHistoryDto>>> GetBorrowHistory(int id)
        {
            var member = await _context.Members
                .Include(m => m.Borrowrecords)
                .ThenInclude(br => br.LibraryItem)
                .FirstOrDefaultAsync(m => m.Id == id);

            if (member == null)
            {
                return NotFound();
            }

           

            var history = member.Borrowrecords.Select(br => new BorrowHistoryDto
            {
                Id = br.Id,
                BookTitle = br.LibraryItem.Title,
                BorrowDate = br.BorrowDate,
                DueDate = br.DueDate,
                ReturnDate = br.ReturnDate,
                Fine = br.Fine,
                Status = br.Status
            }).ToList();

            return Ok(history);
        }

        // GET: api/member/borrowlimit/{id}
        [HttpGet("borrowlimit/{id}")]
        public async Task<ActionResult<int>> GetBorrowLimit(int id)
        {
            var member = await _context.Members.FindAsync(id);
            if (member == null)
            {
                return NotFound();
            }

            int borrowLimit = member.MemberType switch
            {
                "Student" => 5,
                "Staff" => 10,
                "VIP" => 15,
                _ => 0
            };

            return borrowLimit;
        }
    }
}